<template>
  <div class="dashboard">
    <div class="page-container">
      <div class="page-header">
        <h2 class="page-title">仪表盘</h2>
        <p class="page-description">系统概览和关键指标</p>
      </div>

      <!-- 统计卡片 -->
      <el-row :gutter="20" class="stats-row">
        <el-col :xs="24" :sm="12" :md="6">
          <div class="stat-card">
            <div class="stat-icon employee">
              <el-icon><User /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-value">{{ statistics.totalEmployees || 0 }}</div>
              <div class="stat-label">总员工数</div>
            </div>
          </div>
        </el-col>
        <el-col :xs="24" :sm="12" :md="6">
          <div class="stat-card">
            <div class="stat-icon schedule">
              <el-icon><Calendar /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-value">{{ statistics.todaySchedules || 0 }}</div>
              <div class="stat-label">今日排班</div>
            </div>
          </div>
        </el-col>
        <el-col :xs="24" :sm="12" :md="6">
          <div class="stat-card">
            <div class="stat-icon customer">
              <el-icon><UserFilled /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-value">{{ statistics.totalCustomers || 0 }}</div>
              <div class="stat-label">客户总数</div>
            </div>
          </div>
        </el-col>
        <el-col :xs="24" :sm="12" :md="6">
          <div class="stat-card">
            <div class="stat-icon room">
              <el-icon><House /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-value">{{ statistics.totalRooms || 0 }}</div>
              <div class="stat-label">房间总数</div>
            </div>
          </div>
        </el-col>
      </el-row>

      <!-- 图表区域 -->
      <el-row :gutter="20" class="charts-row">
        <el-col :xs="24" :lg="12">
          <div class="chart-card">
            <div class="chart-header">
              <h3>员工类型分布</h3>
            </div>
            <div class="chart-content">
              <v-chart 
                class="chart" 
                :option="employeeTypeChartOption" 
                :loading="chartLoading"
              />
            </div>
          </div>
        </el-col>
        <el-col :xs="24" :lg="12">
          <div class="chart-card">
            <div class="chart-header">
              <h3>本周排班统计</h3>
            </div>
            <div class="chart-content">
              <v-chart 
                class="chart" 
                :option="weeklyScheduleChartOption" 
                :loading="chartLoading"
              />
            </div>
          </div>
        </el-col>
      </el-row>

      <!-- 快捷操作 -->
      <div class="quick-actions">
        <h3>快捷操作</h3>
        <el-row :gutter="16">
          <el-col :xs="24" :sm="12" :md="6">
            <el-card class="action-card" @click="$router.push('/employees/add')">
              <div class="action-content">
                <el-icon class="action-icon"><Plus /></el-icon>
                <span>添加员工</span>
              </div>
            </el-card>
          </el-col>
          <el-col :xs="24" :sm="12" :md="6">
            <el-card class="action-card" @click="$router.push('/schedule')">
              <div class="action-content">
                <el-icon class="action-icon"><Calendar /></el-icon>
                <span>排班管理</span>
              </div>
            </el-card>
          </el-col>
          <el-col :xs="24" :sm="12" :md="6">
            <el-card class="action-card" @click="$router.push('/salary')">
              <div class="action-content">
                <el-icon class="action-icon"><Money /></el-icon>
                <span>工资管理</span>
              </div>
            </el-card>
          </el-col>
          <el-col :xs="24" :sm="12" :md="6">
            <el-card class="action-card" @click="autoScheduleDialog = true">
              <div class="action-content">
                <el-icon class="action-icon"><MagicStick /></el-icon>
                <span>自动排班</span>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>
    </div>

    <!-- 自动排班对话框 -->
    <el-dialog v-model="autoScheduleDialog" title="自动排班" width="400px">
      <el-form :model="autoScheduleForm" label-width="80px">
        <el-form-item label="开始日期">
          <el-date-picker
            v-model="autoScheduleForm.startDate"
            type="date"
            placeholder="选择开始日期"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="结束日期">
          <el-date-picker
            v-model="autoScheduleForm.endDate"
            type="date"
            placeholder="选择结束日期"
            style="width: 100%"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="autoScheduleDialog = false">取消</el-button>
        <el-button type="primary" @click="handleAutoSchedule">开始排班</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { use } from 'echarts/core'
import { CanvasRenderer } from 'echarts/renderers'
import { PieChart, BarChart } from 'echarts/charts'
import {
  TitleComponent,
  TooltipComponent,
  LegendComponent,
  GridComponent
} from 'echarts/components'
import VChart from 'vue-echarts'
import { ElMessage } from 'element-plus'
import { getEmployeeStatistics } from '@/api/employee'
import { autoSchedule } from '@/api/schedule'
import dayjs from 'dayjs'

use([
  CanvasRenderer,
  PieChart,
  BarChart,
  TitleComponent,
  TooltipComponent,
  LegendComponent,
  GridComponent
])

// 统计数据
const statistics = reactive({
  totalEmployees: 0,
  todaySchedules: 0,
  totalCustomers: 0,
  totalRooms: 0,
  fullTime: 0,
  partTime: 0,
  temporary: 0
})

// 图表加载状态
const chartLoading = ref(true)

// 自动排班对话框
const autoScheduleDialog = ref(false)
const autoScheduleForm = reactive({
  startDate: dayjs().format('YYYY-MM-DD'),
  endDate: dayjs().add(7, 'day').format('YYYY-MM-DD')
})

// 员工类型分布图表配置
const employeeTypeChartOption = ref({
  title: {
    text: '员工类型分布',
    left: 'center'
  },
  tooltip: {
    trigger: 'item',
    formatter: '{a} <br/>{b}: {c} ({d}%)'
  },
  legend: {
    orient: 'vertical',
    left: 'left'
  },
  series: [
    {
      name: '员工类型',
      type: 'pie',
      radius: '50%',
      data: [
        { value: 0, name: '全职员工' },
        { value: 0, name: '兼职员工' },
        { value: 0, name: '临时工' }
      ],
      emphasis: {
        itemStyle: {
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)'
        }
      }
    }
  ]
})

// 本周排班统计图表配置
const weeklyScheduleChartOption = ref({
  title: {
    text: '本周排班统计',
    left: 'center'
  },
  tooltip: {
    trigger: 'axis'
  },
  legend: {
    data: ['排班数量']
  },
  xAxis: {
    type: 'category',
    data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      name: '排班数量',
      type: 'bar',
      data: [12, 15, 18, 14, 16, 10, 8],
      itemStyle: {
        color: '#1890ff'
      }
    }
  ]
})

// 加载统计数据
const loadStatistics = async () => {
  try {
    const data = await getEmployeeStatistics()
    statistics.totalEmployees = data.total
    statistics.fullTime = data.fullTime
    statistics.partTime = data.partTime
    statistics.temporary = data.temporary
    
    // 更新员工类型分布图表
    employeeTypeChartOption.value.series[0].data = [
      { value: data.fullTime, name: '全职员工' },
      { value: data.partTime, name: '兼职员工' },
      { value: data.temporary, name: '临时工' }
    ]
    
    // 模拟其他数据
    statistics.todaySchedules = Math.floor(Math.random() * 50) + 20
    statistics.totalCustomers = Math.floor(Math.random() * 100) + 50
    statistics.totalRooms = Math.floor(Math.random() * 20) + 10
    
    chartLoading.value = false
  } catch (error) {
    console.error('加载统计数据失败:', error)
    chartLoading.value = false
  }
}

// 处理自动排班
const handleAutoSchedule = async () => {
  if (!autoScheduleForm.startDate || !autoScheduleForm.endDate) {
    ElMessage.warning('请选择排班日期范围')
    return
  }
  
  try {
    await autoSchedule(autoScheduleForm.startDate, autoScheduleForm.endDate)
    ElMessage.success('自动排班完成')
    autoScheduleDialog.value = false
  } catch (error) {
    console.error('自动排班失败:', error)
  }
}

onMounted(() => {
  loadStatistics()
})
</script>

<style scoped>
.dashboard {
  padding: 0;
}

.page-description {
  color: #666;
  margin: 8px 0 0 0;
}

.stats-row {
  margin-bottom: 24px;
}

.stat-card {
  background: #fff;
  border-radius: 8px;
  padding: 24px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
  display: flex;
  align-items: center;
  transition: box-shadow 0.3s;
}

.stat-card:hover {
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.stat-icon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 16px;
  font-size: 24px;
  color: #fff;
}

.stat-icon.employee {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.stat-icon.schedule {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.stat-icon.customer {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}

.stat-icon.room {
  background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
}

.stat-content {
  flex: 1;
}

.stat-value {
  font-size: 32px;
  font-weight: 600;
  color: #262626;
  line-height: 1;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 14px;
  color: #8c8c8c;
}

.charts-row {
  margin-bottom: 24px;
}

.chart-card {
  background: #fff;
  border-radius: 8px;
  padding: 24px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
  height: 400px;
}

.chart-header {
  margin-bottom: 16px;
}

.chart-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: #262626;
}

.chart-content {
  height: calc(100% - 40px);
}

.chart {
  height: 100%;
  width: 100%;
}

.quick-actions {
  background: #fff;
  border-radius: 8px;
  padding: 24px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.quick-actions h3 {
  margin: 0 0 16px 0;
  font-size: 16px;
  font-weight: 600;
  color: #262626;
}

.action-card {
  cursor: pointer;
  transition: all 0.3s;
  margin-bottom: 16px;
}

.action-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.action-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 16px;
}

.action-icon {
  font-size: 32px;
  color: #1890ff;
  margin-bottom: 8px;
}

.action-content span {
  font-size: 14px;
  color: #262626;
  font-weight: 500;
}

/* 响应式 */
@media (max-width: 768px) {
  .stat-card {
    padding: 16px;
  }
  
  .stat-icon {
    width: 48px;
    height: 48px;
    font-size: 20px;
    margin-right: 12px;
  }
  
  .stat-value {
    font-size: 24px;
  }
  
  .chart-card {
    height: 300px;
    padding: 16px;
  }
  
  .quick-actions {
    padding: 16px;
  }
}
</style>